package com.plian.system.task.indicatorswarning;

import com.plian.Tools.DateUtil;
import com.plian.Tools.UuidUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.config.exception.MyRuntimeException;
import com.plian.system.dao.sys.timing.TimingDao;
import com.plian.system.service.fr.bank.IBankAccountInfoService;
import com.plian.system.service.fr.indicatorswarning.IndicatorsWarningService;
import com.plian.system.vo.fr.bank.BankAccountInfoVO;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;
import java.util.List;

public class IndicatorsWarningTask extends QuartzJobBean {
    @Autowired
    IndicatorsWarningService indicatorsWarningService;
    @Autowired
    TimingDao timingDao;
    @Autowired
    IBankAccountInfoService iBankAccountInfoService;

    private static final Logger log = LoggerFactory.getLogger(IndicatorsWarningTask.class);

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            PageData pageData = new PageData();
            indicatorsWarningService.update(pageData);

            List<BankAccountInfoVO> warn = iBankAccountInfoService.warn();
            for (BankAccountInfoVO vo :warn){
                String id = UuidUtil.get32UUID();
                String orgId = vo.getOrgId();
                //49ade100f5374f2da87c774a7e41e0ae
                String indicatorsId  = "49ade100f5374f2da87c774a7e41e0ae";
                Integer WarningType = 3;
                Integer SourceType = 2;
                Date createTime = DateUtil.now();
                String warningTime = DateUtil.format(createTime, "yyyy-MM");
                int accountNumbers = vo.getAccountNumbers();
                if (accountNumbers > 1){
                    PageData pd = new PageData();
                    pd.put("id",id);
                    pd.put("orgId",orgId);
                    pd.put("warningTime",warningTime);
                    pd.put("indicatorsId",indicatorsId);
                    pd.put("WarningType",WarningType);
                    pd.put("SourceType",SourceType);
                    pd.put("createTime",createTime);
                    pd.put("warningData",accountNumbers+"");
                    //indicatorsWarningService.insertWarn(pd);
                }
            }
            PageData p=new PageData();
            p.put("fun","1");
            p.put("executionTime",new Date());
            timingDao.editByExecutionTime(p);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException("指标定时任务异常");
        }
    }
}
